<?php
if (! isAjaxRequest()) {
    loadTemp("resourceupdater/resourceupdater.temp.php");
    die();
}
if (count($_FILES) == 0) {
    returnMessage("-4", "你没有上传任何文件");
}


//TODO 保留，需要重写这里，将来有需求再来改。
$backmessage = array();
$i = 0;
foreach ($_FILES["file"]["name"] as $value) {
    if ($_FILES["file"]["error"][$i]) {
        $backmessage[$i] = getMessage(- 5);
        continue;
    }
    $back = onFileUpdate($_FILES["file"]["name"][$i], $_FILES["file"]["tmp_name"][$i], $_FILES["file"]["size"][$i]);
    if (is_array($back)) {
        $mess = getMessage(1);
        $mess["data"] = $back;
        $backmessage[$i] = $mess;
    } else {
        $backmessage[$i] = getMessage($back);
    }
    $i ++;
}
die(json_encode($backmessage));

function getMessage($state, $url)
{
    switch ($state) {
        case - 5:
            return array(
                "code" => - 5,
                "message" => "上传失败，请重新上传"
            );
        case - 3:
            return array(
                "code" => - 3,
                "message" => "你必须登录后才能这样做"
            );
        case - 2:
            return array(
                "code" => - 2,
                "message" => "你没有权限这样做"
            );
        case - 1:
            return array(
                "code" => - 1,
                "message" => "数据库上传失败，请联系管理员"
            );
        case 0:
            return array(
                "code" =>0 ,
                "message" => "上传成功",
                "url" => $url
            );
    }
}
?>
